package com.ybbase.framework.common.util;

import java.util.UUID;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 *
 * @author cg
 * @date 2018/7/25
 * @since 4.0
 */

public class StringsUtil {

    public static final String REGEX_CHINESE = "[\u4e00-\u9fa5]";

    public static final String REGEX_ALL_CHINESE = "[\u4e00-\u9fa5]+";

    /**
     * 驼峰转下划线
     *
     * @author cg
     * @date 2018/7/24 18:03
     * @since 4.0
     */
    public static String underscoreName(String camelCaseName) {
        StringBuilder result = new StringBuilder();
        if (camelCaseName != null && camelCaseName.length() > 0) {
            result.append(camelCaseName.substring(0, 1).toLowerCase());
            for (int i = 1; i < camelCaseName.length(); i++) {
                char ch = camelCaseName.charAt(i);
                if (Character.isUpperCase(ch)) {
                    result.append("_");
                    result.append(Character.toLowerCase(ch));
                } else {
                    result.append(ch);
                }
            }
        }
        return result.toString();
    }


    /**
     * 下划线转驼峰
     *
     * @author cg
     * @date 2018/7/24 18:05
     * @since 4.0
     */
    public static String camelCaseName(String underscoreName) {
        StringBuilder result = new StringBuilder();
        if (underscoreName != null && underscoreName.length() > 0) {
            boolean flag = false;
            for (int i = 0; i < underscoreName.length(); i++) {
                char ch = underscoreName.charAt(i);
                if ("_".charAt(0) == ch) {
                    flag = true;
                } else {
                    if (flag) {
                        result.append(Character.toUpperCase(ch));
                        flag = false;
                    } else {
                        result.append(ch);
                    }
                }
            }
        }
        return result.toString();
    }

    /**
     * 获取实体的名字 (过滤前缀)
     *
     * @param tableName 字符串
     * @return String
     * @author shenaiguo
     * @date 2019-12-5
     */
    public static String getClassName(String tableName) {
        int index =tableName.indexOf('_');
        if (index!=-1){
            return camelCaseName(tableName.substring(index,tableName.length()));
        }
        return tableName;
    }

    /**
     * 获取uuid
     *
     * @author cg
     * @since 4.0
     */
    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 截取图片的名字
     *
     * @param str 字符串
     * @return String
     * @author met
     * @date 2019/10/8 6:55 下午
     * @since 4.0
     */
    public static String getFileName(String str) {
        String subString = null;
        if (str != null) {
            int value = str.lastIndexOf("\\");
            subString = str.substring(value + 1, str.length());
        }
        return subString;
    }

    /**
     * 验证字符串是是否包含汉字
     *
     * @param str 要验证的字符串
     * @return boolean
     * @author yanBin
     * @date 2019/8/27 17:36
     * @since 4.0
     */
    public static boolean isChinese(String str) {
        Pattern pat = Pattern.compile(REGEX_CHINESE);
        return pat.matcher(str).find();
    }

    /**
     * 字符串是否全部为中文
     *
     * @param str 验证的字符串
     * @return true 全部为中文
     * @since 4.0
     */
    public static boolean isAllChinese(String str) {
        return str.matches(REGEX_ALL_CHINESE);
    }

    /**
     * 使用java正则表达式去掉多余的.与0
     *
     * @param str 字符串
     * @return 过滤的字符串
     * @since 4.0
     */
    public static String splitZero(String str) {
        if (str != null) {
            if (str.indexOf(".") > 0) {
                str = str.replaceAll("0+?$", "");
                str = str.replaceAll("[.]$", "");
            }
        }
        return str;
    }

    /**
     * 获取字符串包含字母
     *
     * @param str 字符串
     * @return 过滤的字符串
     * @since 4.0
     */
    public static String getLetter(String str) {
        StringBuffer sb = new StringBuffer();
        if (str!=null) {
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                boolean b = (c <= 'z' && c >= 'a') || (c <= 'Z' && c >= 'A');
                if (b) {
                    sb.append(c);
                }
            }
        }
        return sb.toString();
    }



}